//
//  WBNavigationController.swift
//  mySwiftVV
//
//  Created by terrylee on 2016/12/10.
//  Copyright © 2016年 terry. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //隐藏默认的 NavigationBar 
        navigationBar.isHidden = true
    }

    
    //所有的push方法,都会调用这个方法
    //viewController 是被push的控制器,设置他的左侧按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //如果不是栈底的控制器才需要隐藏,根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                //判断控制器的级数, 只有一个子控制器的时候,显示底控制器的标题
                if childViewControllers.count == 1 {
                    
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                
                //取出 自定义的navItem, 设置左侧按钮为返回按钮
//                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent))
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, fontSize: 16, target: self, action: #selector(popToParent), isBackButton: true)
            }
        }
        
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc fileprivate func popToParent(){
        
        popViewController(animated: true)
        
        
        
    }

}
